class Node{//节点
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null;
    }
}
class DoubleList{
    constructor(){
        this.head = null;//首个节点
        this.tail = null;//尾部节点
        this.count = 0;//节点数量
    }
    push(element){//向链表尾部添加⼀个新元素。
        let node = new Node(element);
        if(this.head === null){//为空时
            this.head = node;
            this.tail = node;
        }else{
            let last = this.tail;
            this.tail = node;
            last.next = node;
            node.prev = last;
        }
        this.count++;
    }
    getElementAt(index){//查找指定元素
        let current = this.head;
        for(let i = 0;i < index;i++){
            current = current.next;
        }
        return current
    }
    insert(element,index){//向链表的特定位置插⼊⼀个新元素
        if(index >= 0 && index <= this.count){
            let node = new Node(element);
            if(index === 0){
                if(this.head === null){//为空时
                    this.head = node;
                    this.tail = node;
                }else{//不为空时
                    let current = this.head;
                    node.next = current;
                    current.prev = node;
                    this.head = node;
                }
            }else if(index === this.count){
                let last = this.tail;
                last.next = node;
                node.prev = last;
            }else{
                let prev = this.getElementAt(index - 1);
                let current = prev.next;
                prev.next = node;
                node.prev = prev;
                current.prev = node;
                node.next = current;
            }
            this.count++;
        }else{
            return false
        }
    }
    size(){//元素节点的个数
        return this.count;
    }
    isEmpty(){//链表是否为空
        return this.count === 0
    }
}
let l = new DoubleList()
l.push("a")
l.getElementAt("a")
l.insert("b")
console.log(l);